﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Ccit.ClassLibrary;
using Newtonsoft.Json;
using System.Drawing;
using DevExpress.Data.Filtering;

namespace Ccit.SysPlat.Organ
{
    /// <summary>
    /// Description: 系统平台 -> 组织机构 -> 用户列表
    /// Author: tim 2010-02-10
    /// </summary>
    public partial class sys_user_list : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _hidOrganId.Value = (null != Request.QueryString["organId"]) ? Request.QueryString["organId"].ToString() : "1";
                _hidDelSign.Value = (null != Request.QueryString["delSign"]) ? Request.QueryString["delSign"].ToString() : "0";
                MsUser whereClause = new MsUser {
                    delSign = (eTrueFalse)Enum.Parse(typeof(eTrueFalse), _hidDelSign.Value),
                    belongOrgan = new MsOrgan {
                        organId = _hidOrganId.Value
                    }
                };
                ViewState["whereClause"] = JavaScriptConvert.SerializeObject(whereClause);
                whereClause = null;
                initializePage();
            }
            bindGridView();
        }

        private void initializePage()
        {
            setGridProperty();
        }

        private void setGridProperty()
        {
            grid.Settings.ShowFilterRow = true;
            grid.Settings.ShowGroupPanel = true;
            grid.Settings.ShowGroupFooter = DevExpress.Web.ASPxGridView.GridViewGroupFooterMode.Hidden;
        }

        private void bindGridView()
        {
            grid.DataSource = GetData();
            grid.DataBind();
        }

        private DataSet GetData()
        {            
            UserBLL userBLL = new UserBLL();
            MsUser whereClause = JavaScriptConvert.DeserializeObject(ViewState["whereClause"].ToString(), typeof(MsUser)) as MsUser;
            DataSet dSet = userBLL.GetUserList(whereClause);
            whereClause = null;
            userBLL.Release();
            userBLL = null;
            return dSet;
        }

        protected void grid_RowCreated(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e)
        {
            if (e.RowType != DevExpress.Web.ASPxGridView.GridViewRowType.Data) return;
            string userId = grid.GetRowValues(e.VisibleIndex, "USERID").ToString();  //主键
            string delSign = (string)grid.GetRowValues(e.VisibleIndex, "DELSIGN");  //有效标志
            string orderNo = grid.GetRowValues(e.VisibleIndex, "ORDERNO").ToString();   //显示顺序
            #region 显示顺序列
            LinkButton lnbMoveUp = (LinkButton)grid.FindRowCellTemplateControl(e.VisibleIndex, null, "lnbMoveUp");
            LinkButton lnbMoveDown = (LinkButton)grid.FindRowCellTemplateControl(e.VisibleIndex, null, "lnbMoveDown");

            lnbMoveUp.Attributes["onclick"] = "javascript:doMove('" + userId + "','" + orderNo + "','0')";
            lnbMoveDown.Attributes["onclick"] = "javascript:doMove('" + userId + "','" + orderNo + "','1')";

            #endregion


            #region 操作列
            LinkButton lnbModifyRec = (LinkButton)grid.FindRowCellTemplateControl(e.VisibleIndex, null, "lnbModifyRec");
            LinkButton lnbDel = (LinkButton)grid.FindRowCellTemplateControl(e.VisibleIndex, null, "lnbDel");
            LinkButton lnbResume = (LinkButton)grid.FindRowCellTemplateControl(e.VisibleIndex, null, "lnbResume");

            if (delSign == "0")
            {
                lnbResume.Visible = false;
                lnbModifyRec.Attributes["onclick"] = "javascript:return doEdit('" + userId + "')";
                lnbDel.Attributes["onclick"] = "javascript:return doDelResume('" + userId + "','0')";
            }
            else
            {
                lnbModifyRec.Visible = false;
                lnbDel.Visible = false;
                lnbResume.Attributes["onclick"] = "javascript:return doDelResume('" + userId + "','1')";
            }
            #endregion

            e.Row.Cells[7].Text = (delSign == "0") ? "正常" : "删除";
            e.Row.Cells[7].ForeColor = (delSign == "0") ? Color.Blue : Color.Red;            
        }


        #region ajax methods
        
        /// <summary>
        /// 恢复用户记录
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string Resume(string userId)
        {
            UserBLL userBLL = new UserBLL();
            List<string> lstResult = userBLL.Resume(userId);
            var newObj = new {
                OPERTYPE = "RESUME",
                RSTSIGN = lstResult[0],
                RSTMSG = lstResult[1]
            };
            string serExpr = JavaScriptConvert.SerializeObject(newObj);
            newObj = null;
            lstResult = null;
            userBLL.Release();
            userBLL = null;
            return serExpr;
        }

        /// <summary>
        /// 上下移动调整显示顺序
        /// </summary>
        /// <param name="userId">用户记录ID</param>
        /// <param name="curOrder">调整前的显示顺序</param>
        /// <param name="moveType">移动类型0:上移; 1:下移</param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string ModeSingleStep(string userId, string curOrder, string moveType)
        {            
            UserBLL userBLL = new UserBLL();
            List<string> lstResult = null;
            if (moveType == "0")
                lstResult = userBLL.MoveUp(userId, curOrder);
            else
                lstResult = userBLL.MoveDown(userId, curOrder);

            var newObj = new {
                OPERTYPE = "SINGLEMOVE",
                RSTSIGN = lstResult[0],
                RSTMSG = lstResult[1]
            };
            string serExpr = JavaScriptConvert.SerializeObject(newObj);

            newObj = null;
            lstResult = null;
            userBLL.Release();
            userBLL = null;
            return serExpr;
        }

        /// <summary>
        /// 批量删除用户
        /// </summary>
        /// <param name="userIds">用户ID串</param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string BatDelUsers(string userIds)
        {
            UserBLL userBLL = new UserBLL();
            List<string> lstResult = userBLL.Delete(userIds);
            var newObj = new {
                OPERTYPE = "DELETE",
                RSTSIGN = lstResult[0],
                RSTMSG = lstResult[1]
            };
            string serExpr = JavaScriptConvert.SerializeObject(newObj);
            newObj = null;
            lstResult = null;
            userBLL.Release();
            userBLL = null;
            return serExpr;
        }

        /// <summary>
        /// 恢复用户默认口令
        /// </summary>
        /// <param name="userIds"></param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string ResumePwd(string userIds)
        {
            UserBLL userBLL = new UserBLL();
            List<string> lstResult = userBLL.ResumeDefPwd(userIds);
            var newObj = new
            {
                OPERTYPE = "RESUMEPWD",
                RSTSIGN = lstResult[0],
                RSTMSG = lstResult[1]
            };
            string serExpr = JavaScriptConvert.SerializeObject(newObj);
            newObj = null;
            lstResult = null;
            userBLL.Release();
            userBLL = null;
            return serExpr;
        }
        #endregion

        protected void lnbCallBack_Click(object sender, EventArgs e)
        {
            grid.Selection.UnselectAll();
        }

        protected void lnbSel_Click(object sender, EventArgs e)
        {
            string selALLSign = _hidSelALLSign.Value;
            if (selALLSign.Equals("0")) { //取消选择
                grid.Selection.UnselectAll();
            }
            else { //选择全部
                grid.Selection.SelectAll();
            }
        }

        protected void grid_ProcessColumnAutoFilter(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewAutoFilterEventArgs e)
        {
            e.Criteria = null;
            e.Criteria = new BinaryOperator(new OperandProperty(e.Column.FieldName.ToString()), new OperandValue("%" + e.Value + "%"), BinaryOperatorType.Like);
            if (!string.IsNullOrEmpty(e.Value.ToString()) && e.Value.ToString().Substring(0, 1) == "%")
                e.Value = e.Value.ToString().Substring(1);
        }
    }
}
